package com.xk857.service.impl;

import com.google.gson.Gson;
import com.xk857.db.CarDB;
import com.xk857.db.UserDB;
import com.xk857.pojo.*;
import com.xk857.pojo.req.UserREQ;
import com.xk857.pojo.req.WeatherREQ;
import com.xk857.pojo.vo.CarStatus;
import com.xk857.service.CarService;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * 小车服务层实现类
 * 由于没有连接数据库，因此包含了Dao层的操作
 *
 * @Author CV大魔王
 * @Date 2020.1.20
 */
@Service
public class CarServiceImpl implements CarService {

    @Override
    public Car findCarById(Integer id) {
        for (Car c : CarDB.carList) {
            if (c.getCarId().equals(id)) {
                return c;
            }
        }
        return null;
    }

    @Override
    public CarStatus setCarAction(CarStatus car) {
        CarDB.carList.forEach(car1 -> {
            if (car1.getCarId().equals(car.getCarId())) {
                car.setCarAction(car.getCarAction());
            }
        });

        return car;
    }

    @Override
    public void addMoney(Car car, Integer money) {
        CarDB.carList.forEach(car1 -> {
            if (car1.getCarId().equals(car.getCarId())) {
                car.setMoney(car.getMoney() + money);
            }
        });
    }

    @Override
    public TrafficLightConfiguration findTrafficLightConfigurationById(Integer trafficLightId) {
        for (TrafficLightConfiguration traffic : CarDB.intersectionList) {
            if (traffic.getTrafficLightId().equals(trafficLightId)) {
                return traffic;
            }
        }
        return null;
    }

    @Override
    public List<TrafficLightConfiguration> findTrafficLightConfigurationAll() {
        return CarDB.intersectionList;
    }

    @Override
    public boolean login(UserREQ user) {
        List<User> userList = UserDB.userList;
        for (User user1 : userList) {
            if (user.getUsername().equals(user1.getUsername()) && user.getPassword().equals(user1.getPassword())) {
                return true;
            }
        }
        return false;
    }

    @Override
    public Weather getWeather(WeatherREQ req) {
        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url("http://iweather.market.alicloudapi.com/history?city=" + req.getCity() + "&date=" + req.getDate() + "&prov=" + req.getProv() + "")
                .header("Authorization", "APPCODE 8e0fcc308680470693a2b0090f20cfa3").build();
        Response response = null;
        Weather weather = null;
        try {
            response = client.newCall(request).execute();
            if (response.isSuccessful()) {
                assert response.body() != null;
                String string = response.body().string();

                Gson gson = new Gson();
                WeatherJson weatherJson = gson.fromJson(string, WeatherJson.class);

                // 封装返回结果
                weather = new Weather();
                weather.setProvinces(weatherJson.getData().getCityinfo().getProvinces());
                weather.setCity(weatherJson.getData().getCityinfo().getCity());
                Weather.Detail detail = null;
                List<Weather.Detail> detailList = new ArrayList<>();
                for (WeatherJson.DataDTO.DetailDTO dto : weatherJson.getData().getDetail()) {
                    detail = new Weather.Detail();
                    detail.setIdate(dto.getIdate());
                    detail.setWeek("周"+dto.getWeek());
                    detail.setNightAirTemperature(dto.getNight_air_temperature());
                    detail.setDayAirTemperature(dto.getDay_air_temperature());
                    detail.setWeather(dto.getWeather());
                    detail.setHumidity(dto.getHumidity());
                    detail.setQuality(dto.getQuality());
                    detail.setAqiPm25(dto.getAqi_pm25());
                    detail.setUltravioletRaysStatus(dto.getUltraviolet_rays_status());
                    detailList.add(detail);
                }
                weather.setDetailList(detailList);
                return weather;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


}
